<template>
    <el-button
            @click="handleClick"
            :size="size"
            :autofocus="autofocus"
            :loading="loading"
            :plain="plain"
            :round="round"
            :icon="icon"
            :circle="circle"
            :nativeType="nativeType"
            :disabled="disabled"
            v-bind:class="disabled?'common-disabled':''"
            :type="type"
            class="btn commonButton">
        {{text}} <slot></slot>
    </el-button>
</template>
<script>
    import Service from '../../../services/common.service'

    export default {
        lock: Boolean,
        _params: Object,
        _url: String,
        clickTime: Number,
        data() {
            return {}
        },
        created: function () {
        },
        methods: {
            handleClick(evt) {
                const me = this;
                if (me.lock) {
                    return;
                }
                if (me.limitTime && (new Date().getTime() - me.clickTime < me.limitTime)) {
                    return;
                }
                me.clickTime = new Date().getTime();
                me.$emit('click', evt, me);
                if (me.autopost) {
                    me.post();
                }
            },
            post(url, params) {
                const me = this;
                if (me.lock) {
                    return;
                }
                me._params = params ? params : me.params;
                me._url = url ? url : me.url;

                if (me._url && me._params) {
                    me.lock = true;
                    Service.post(me._url, me._params).then(res => {
                        if (res.data.succeeded == true) {
                            me.$emit('callback', res);
                        }
                        me.lock = false;
                    })
                }

            }
        },
        props: {
            type: {
                type: String,
                default: 'default'
            },
            size: {
                type: String,
                default: ''
            },
            icon: {
                type: String,
                default: ''
            },
            nativeType: {
                type: String,
                default: 'button'
            },
            loading: Boolean,
            disabled: Boolean,
            plain: Boolean,
            autofocus: Boolean,
            round: Boolean,
            circle: Boolean,
            text: String,
            url: String,
            params: Object,
            autopost: {
                type: Boolean,
                default: false
            },
            limitTime: {
                type: [Number, Function],
                default: 0
            }
        },
    }
</script>
